package com.sinry.common.core.domain.vo.base;

import com.sinry.common.core.enums.Response;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author linxinlei
 * @project sinry-cloud
 * @description 公共响应体
 * @date 2022/5/22
 **/
@ApiModel(value = "公共响应信息", description = "公共响应信息")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class R<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    public static final boolean SUCCESS = true;
    public static final boolean FAIL = false;

    /**
     * 响应状态
     */
    @ApiModelProperty(value = "响应状态")
    private boolean status;
    /**
     * 响应码
     */
    @ApiModelProperty(value = "响应码")
    private Integer code;
    /**
     * 响应信息
     */
    @ApiModelProperty(value = "响应信息")
    private String message;
    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;


    public static <T> R<T> of() {
        return new R<>();
    }

    public static <T> R<T> ok(T data) {
        Response responseCode = Response.OK;
        return new R<>(SUCCESS, responseCode.getCode(), responseCode.getMessage(), data);
    }

    public static <T> R<T> fail(T data) {
        Response responseCode = Response.FAIL;
        return new R<>(FAIL, responseCode.getCode(), responseCode.getMessage(), data);
    }
}
